package prj.common.bean;

import java.util.regex.Pattern;

import javax.faces.component.UIInput;
import javax.faces.event.AjaxBehaviorEvent;
import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import prj.common.service.Mailer;

@Named("contactMe")
@Scope("request")
public class ContactMe extends CaptchaSupportedBean {
	private static final long serialVersionUID = 1L;

	public static final Pattern EMAIL_PATTERN = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
	
	private String subject;
	private String replyTo;
	private String msg;
	private String subjectError;
	private String replyToError;
	private String msgError;
	private Mailer mailer;
	
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getReplyTo() {
		return replyTo;
	}
	public void setReplyTo(String replyTo) {
		this.replyTo = replyTo;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getSubjectError() {
		return subjectError;
	}
	public void setSubjectError(String subjectError) {
		this.subjectError = subjectError;
	}
	public String getReplyToError() {
		return replyToError;
	}
	public void setReplyToError(String replyToError) {
		this.replyToError = replyToError;
	}
	public String getMsgError() {
		return msgError;
	}
	public void setMsgError(String msgError) {
		this.msgError = msgError;
	}
	@Autowired(required=false)
	public void setMailer(Mailer mailer) {
		this.mailer = mailer;
	}
	public void validate(AjaxBehaviorEvent e) {
		super.validate(e);
		UIInput input = (UIInput)e.getComponent();
		String value = (String)input.getValue();
		if (input.getId().equals("subject")) validateSubject(value);
		else if (input.getId().equals("reply-to")) validateReplyTo(value);
		else if (input.getId().equals("msg-body")) validateMsg(value);
	}
	public void validateSubject(String subject) {
		String error = null;
		subjectError = null;
		
		if (subject.trim().length() == 0) error = "contactme_subject_error_required";
		if (error != null) subjectError = handleFormMessage(error);
	}
	public void validateReplyTo(String replyTo) {
		String error = null;
		replyToError = null;
		
		if (replyTo.trim().length() == 0) error = "contactme_replyto_error_required";
		else if (!ContactMe.EMAIL_PATTERN.matcher(replyTo).matches()) error = "contactme_replyto_error_invalid";
		if (error != null) replyToError = handleFormMessage(error);
	}
	public void validateMsg(String msg) {
		String error = null;
		msgError = null;
		
		if (msg.trim().length() == 0) error = "contactme_msg_error_required";
		else if (msg.trim().length() > 5000) error = "contactme_msg_error_maxed";
		if (error != null) msgError = handleFormMessage(error);
	}
	public void sendMessage(AjaxBehaviorEvent e) {
		//Validate all inputs;
		validateSubject(subject);
		validateReplyTo(replyTo);
		validateMsg(msg);
		validateCaptcha(getCaptchaValue());
		
		if (subjectError == null && replyToError == null && msgError == null && getCaptchaError() == null) {
			try {
				mailer.sendMail(replyTo,subject,msg);
				setFormMsgSuccess(handleFormMessage("contactme_form_msg_success"));
			} catch (Exception ex) {
				setFormMsgError(handleFormMessage("contactme_form_msg_error_exception"));
				logger.error(getFormMsgError(),ex);
			}
		}
		
	}
	@Override
	public void setCaptchaField() {
		captchaField = "contactme-captcha";
	}
}
